﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Zwms.Client.CommonUI;
using Zwms.Data.CommonObject;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.InfoWms
{
    public partial class ShelfForm : BaseForm
    {
        #region 成员变量
        public WhShelf CurrentRecord { get; set; }
        private EnumOpenType OpenType { get; set; }
        #endregion

        public ShelfForm(EnumOpenType opentype, string sShelfCode,string sWarehouseCode,string sAreaCode)
        {
            InitializeComponent();
            OpenType = opentype;

            if (opentype == EnumOpenType.New)
            {
                CurrentRecord = new WhShelf();
            }
            else if (opentype == EnumOpenType.Open)
            {
               tbShelfCode.ReadOnly = true;
                //通过服务读取'入库计划明细'的实例
                ServiceReferenceZwms.ServiceZwmsClient client =
                    new ServiceReferenceZwms.ServiceZwmsClient();
                WhShelf record = new WhShelf();
                record.ShelfCode = sShelfCode;
                record.WarehouseCode = sWarehouseCode;
                record.AreaCode = sAreaCode;
                CurrentRecord = client.QueryShelf(record);
            }
            InitBtnImage();
            InitControl();
        }

        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnClear.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
        }

        private void InitControl()
        {
            if (this.OpenType == EnumOpenType.New)
            {
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadInfo(CurrentRecord);
            }
        }

        private void LoadInfo(WhShelf record)
        {
            this.tbShelfCode.Text = record.ShelfCode.Trim();
            this.tbShelfName.Text = record.ShelfName.Trim();
            this.cmbShelfType.SelectedIndex = this.cmbShelfType.Items.IndexOf(record.ShelfType);
            this.tbsArea.SetTextBoxContent(record.AreaName);
            this.tbsArea.Tag = record.AreaCode;
            this.tbWarehouse.Text = record.WarehouseName;
            this.tbWarehouse.Tag = record.WarehouseCode;
      
            this.tbRemark.Text = record.Remark;

            //------------------------------------------------------
           ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
            DateTime dtNow = client1.GetCurrentSystemTime();

            this.tbCreateUser.Text = record.CreateUserName;
            this.dpuCreateDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.CreateDate + record.CreateTime));
            this.tbEditUser.Text = record.EditUserName;
            this.dpuEditDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.EditDate + record.EditTime));

        }

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.OpenType == EnumOpenType.New)
            {
                if (tbShelfCode.Text.Trim() == "")
                {
                    bRet = false;
                    this.tbShelfCode.BackColor = Color.Yellow;
                }
            }
            if (tbShelfName.Text.Trim() == "")
            {
                bRet = false;
                this.tbShelfName.BackColor = Color.Yellow;
            }
            if (tbsArea.GetTextBoxContent() == "")
            {
                bRet = false;
                this.tbsArea.SetTextBGColor(Color.Yellow);
            }
            
            if (tbWarehouse.Text == "")
            {
                bRet = false;
                this.tbWarehouse.BackColor = Color.Yellow;
            }

            if (!bRet)
            {
                return;
            }
            if (MessageBox.Show("您确定要添加（修改）货架信息吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {

                WhShelf record = new WhShelf();
                record.ShelfCode =tbShelfCode.Text;
                record.ShelfName = tbShelfName.Text;
                if (cmbShelfType.SelectedIndex != -1)
                {
                    record.ShelfType = cmbShelfType.SelectedItem.ToString();
                } 
                if (tbsArea.Tag != null)
                {
                    record.AreaName = tbsArea.GetTextBoxContent();
                    record.AreaCode = tbsArea.Tag.ToString();
                }
                else
                {
                    record.AreaName = "";
                    record.AreaCode = "";
                }

                record.WarehouseName = tbWarehouse.Text;
                record.WarehouseCode = tbWarehouse.Tag.ToString();

                record.Remark = tbRemark.Text;

                //------------------------------------
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                if (this.OpenType == EnumOpenType.New)
                {
                    record.CreateUserName = MainForm.CurrentUser.UserName;
                    record.CreateUserCode = MainForm.CurrentUser.Id;
                    record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                    record.EditUserName = "";
                    record.EditUserCode = "";
                    record.EditDate = "";
                    record.EditTime = "";
                }
                else
                {
                    record.EditUserName = MainForm.CurrentUser.UserName;
                    record.EditUserCode = MainForm.CurrentUser.Id;
                    record.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                    record.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
                }

                ServiceReferenceZwms.ServiceZwmsClient client
                    = new ServiceReferenceZwms.ServiceZwmsClient();

                if (this.OpenType == EnumOpenType.New)
                {
                    bRet = client.AddShelf(record);
                }
                else
                {
                    bRet = client.UpdateShelf(record);
                }
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.OpenType == EnumOpenType.New)
            {
                tbShelfCode.Text = "";
            }

            tbShelfName.Text = "";
            cmbShelfType.SelectedIndex = -1;

            tbsArea.SetTextBoxContent("");
            tbsArea.Tag = "";
            tbWarehouse.Text = "";
            tbWarehouse.Tag = "";

            tbRemark.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void Control_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender);
        }

        private void Control_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender);
        }

        private void tbsArea_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.AreaWnd uc = new AreaWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "库区选择");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsArea.SetTextBoxContent(uc.SelectedRecord.AreaName);
                tbsArea.Tag = uc.SelectedRecord.AreaCode;

                tbWarehouse.Text = uc.SelectedRecord.WarehouseName;
                tbWarehouse.Tag = uc.SelectedRecord.WarehouseCode;
            }
        }
        #endregion
    }
}
